<template>
	<page-meta :page-style="$theme.pageStyle">
		<!-- #ifndef H5 -->
		<navigation-bar :front-color="$theme.navColor" :background-color="$theme.navBgColor" />
		<!-- #endif -->
	</page-meta>
	<view class="news-detail bg-white">
		<view class="hexiao-card bg-white px-[20rpx] py-[32rpx]" v-for="(item, index) in formData" :key="index" @click="setchecked(index, item.id, item.shengyu)">
			<view class="news-card-content">
				<view class="news-card-content-title text-base">{{ item.title }}</view>
				<view class="news-card-content-intro text-gray-400 text-sm mt-[16rpx]">
					{{ item.content }}
				</view>
				<view class="flex justify-between">
					<view>
						<view class="text-muted text-xs w-full mt-[16rpx]">剩余次数：{{ item.shengyu }}</view>
						<view class="text-muted text-xs w-full mt-[16rpx]">使用次数：{{ item.shiyong_num }}</view>
					</view>
					<radio :disabled="item.shengyu == 0" :value="item.id" :checked="index === current" />
				</view>
			</view>
		</view>
		<view v-if="formData.length==0" class="flex items-center justify-center px-[20rpx] py-[32rpx]">
			暂无可使用项目
		</view>

		<view class="mt-[100rpx] mx-[26rpx]">
			<u-button type="primary" shape="circle" @click="submit">立即核销</u-button>
		</view>
	</view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { hexiao1, hexiao2 } from '@/api/shop';

let current = ref<any>('');
let hid = ref<any>('');
const formData = ref<any>([]);

const postData = reactive<any>({
	uid: '',
	qy_id: '',
	remark:''
});

const getData = async (uid, sid) => {
	formData.value = await hexiao1({ uid: uid, sid: sid });
};

const setchecked = async (index, id, num) => {
	if (num == 0) return uni.$u.toast('该项目已使用');
	current.value = index;
	postData.qy_id = id;
};

const submit = async () => {
	if (!postData.qy_id) return uni.$u.toast('请选择核销项目');
	await hexiao2(postData);
	uni.$u.toast('核销成功');
	setTimeout(() => {
		uni.navigateBack();
	}, 1500);
};

onLoad((options: any) => {
	postData.uid = options.uid;
	getData(options.uid, options.sid);
});
</script>

<style lang="scss">
.news-detail {
	height: 100%;
	background: rgba(255, 255, 255, 0.95);

	.hexiao-card {
		border-bottom: 1px solid #f8f8f8;
	}
}
</style>
